#include <stdio.h>
#include <vector>
#include <algorithm>

void sumlen(int x, int &sum, int &len) {
  sum = len = 0;
  while (x > 0) {
    sum += x % 10;
    len++;
    x /= 10;
  }
}

struct Elem {
  int value, sum, len;
  bool operator < (Elem s) {
    return sum < s.sum || sum == s.sum && value < s.value;
  }
};

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int p = 1;
  for (int i = 1; i <= n; i++) {
    p *= 10;
  }  
  std::vector<Elem> a(p);
  for (int i = 0; i < p; i++) {
    a[i].value = i + 1;
    sumlen(a[i].value, a[i].sum, a[i].len);
  }
  std::sort(a.begin(), a.end());

  // f[len][sum]
  std::vector<std::vector<int> > f(n + 2, std::vector<int>(9 * (n + 1) + 1, -1));
  for (int i = 0; i < m; i++) {
    int k, sum, len;
    scanf("%d", &k);
    sumlen(k, sum, len);
    if (f[len][sum] < 0){
      int ans = 0;
      for (int j = 0; a[j].value != k; j++)
        if (a[j].len > len)
          ans++;
      f[len][sum] = ans;
    }
    printf("%d ", f[len][sum]);
  }
}
